package com.lzy.wzy.controller;

import com.lzy.wzy.constant.MyConstant;
import com.lzy.wzy.result.Result;
import com.lzy.wzy.service.MyFlowableService;
import com.lzy.wzy.token.MyUsernamePasswordToken;
import com.lzy.wzy.utils.PatternUtils;
import com.lzy.wzy.vo.ApprovalDetailsVo;
import com.lzy.wzy.vo.WorkOrderTaskVo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;


@RequestMapping("/flow")
@RestController
public class FlowController {

    @Autowired
    private MyFlowableService myFlowableService;


    @PreAuthorize("hasAnyAuthority('ROLE_PLAN','ROLE_QUALITY','QUALITY_INSPECTION','ROLE_CRAFT_DIRECTOR','ROLE_PRODUCE_DIRECTOR','ROLE_PROCESS_ENGINEER','ROLE_QUALITY_ENGINEER','ROLE_SUPERVISOR','ROLE_ADMIN')")
    @RequestMapping(value = "/approve/{workOrderNumber}/{taskId}",method = RequestMethod.GET)
    public Result<T> approve(@NotBlank @PathVariable("workOrderNumber") String workOrderNumber, @NotBlank @PathVariable("taskId") String taskId) {
        synchronized (MyConstant.Re.re){
            if (!PatternUtils.checkWorkOrderNumber(workOrderNumber)) {
                return Result.invalidFormat("非法工单");
            }
            return myFlowableService.doApprove(taskId,workOrderNumber);
        }
    }

    @PreAuthorize("hasAnyAuthority('ROLE_PLAN','ROLE_QUALITY','QUALITY_INSPECTION','ROLE_CRAFT_DIRECTOR','ROLE_PRODUCE_DIRECTOR','ROLE_PROCESS_ENGINEER','ROLE_QUALITY_ENGINEER','ROLE_SUPERVISOR','ROLE_ADMIN')")
    @RequestMapping(value = "reject/{workOrderNumber}/{taskId}",method = RequestMethod.GET)
    public Result<T> reject(@NotBlank @PathVariable("workOrderNumber") String workOrderNumber, @NotBlank @PathVariable("taskId") String taskId) {
        synchronized (MyConstant.Re.r2) {
            if (!PatternUtils.checkWorkOrderNumber(workOrderNumber)) {
                return Result.invalidFormat("非法工单");
            }
            return myFlowableService.doReject(taskId, workOrderNumber);
        }
    }


    @PreAuthorize("hasAnyAuthority('ROLE_PLAN','ROLE_QUALITY','QUALITY_INSPECTION','ROLE_CRAFT_DIRECTOR','ROLE_PRODUCE_DIRECTOR','ROLE_PROCESS_ENGINEER','ROLE_QUALITY_ENGINEER','ROLE_SUPERVISOR','ROLE_ADMIN','ROLE_OTHER')")
    @RequestMapping(value = "/loadMore/{pageNum}/{pageSize}",method = RequestMethod.GET)
    public Result<List<WorkOrderTaskVo>> loadMore(@PathVariable("pageNum")int pageNum,@PathVariable("pageSize")int pageSize){
        MyUsernamePasswordToken token=(MyUsernamePasswordToken) SecurityContextHolder.getContext().getAuthentication();
        return Result.success(myFlowableService.doGetAnAgentTask((String) token.getPrincipal(),pageNum,pageSize));
    }


    @PreAuthorize("hasAnyAuthority('ROLE_PLAN','ROLE_QUALITY','QUALITY_INSPECTION','ROLE_CRAFT_DIRECTOR','ROLE_PRODUCE_DIRECTOR','ROLE_PROCESS_ENGINEER','ROLE_QUALITY_ENGINEER','ROLE_SUPERVISOR','ROLE_ADMIN','ROLE_OTHER')")
    @RequestMapping(value = "/getDetail/{proInstId}",method = RequestMethod.GET)
    public Result<ApprovalDetailsVo> getApproveDetails(@PathVariable("proInstId")String proInstId){
        return myFlowableService.doGetApproveDetails(proInstId);
    }
}
